package ter.find.pattern.gui;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

import ter.find.pattern.BaseSymbolClassModel;


public class SymbolClassPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
    private TableRowSorter<BaseSymbolClassModel> sorter;

    public SymbolClassPanel(BaseSymbolClassModel contentModel) {
        super();
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.setVisible(true);
        //this.setSize(new Dimension(500, 50));
        sorter = new TableRowSorter<BaseSymbolClassModel>(contentModel);
        table = new JTable(contentModel);
        table.setRowSorter(sorter);
        
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //table.setDefaultRenderer(Object.class, new TableColorLine());
        ListSelectionModel cellSelectionModel = table.getSelectionModel();
        cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent e) {
        	  Object selectedData = null;

            int[] selectedRow = table.getSelectedRows();
            int[] selectedColumns = table.getSelectedColumns();

            for (int i = 0; i < selectedRow.length; i++) {
              for (int j = 0; j < selectedColumns.length; j++) {
                selectedData = table.getValueAt(selectedRow[i], selectedColumns[j]);
              }
            }
            System.out.println("Selected: " + selectedData);
          }

        });
        
        elementCenter(table);
        fixedWidthCol(table, 0, 120);
        fixedWidthCol(table, 1, 430);
        fixedWidthCol(table, 2, 430);
        colorHead(table, Color.green);
        //table.setSize(new Dimension(500, 20));
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setSize(new Dimension(500, 50));
        //Add the scroll pane to this panel.
        add(scrollPane);

    }
    
    public void colorHead(JTable tab,Color c){
		JTableHeader header = tab.getTableHeader();
		header.setBackground(c);
		header.setForeground(Color.black);
		header.setOpaque(true);
	}
    
    public void elementCenter(JTable tab){
    	DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    	centerRenderer.setHorizontalAlignment( JLabel.CENTER );
    	centerRenderer.setPreferredSize(new Dimension(500, 20));
    	
    	tab.setDefaultRenderer(String.class, centerRenderer);
    }

    public void fixedWidthCol(JTable tab,int colIndex,int colWidth ){
    	tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         TableColumn col = tab.getColumnModel().getColumn(colIndex);         
         col.setPreferredWidth(colWidth);
         
    }

}
